import type { SidebarsConfig } from "@docusaurus/plugin-content-docs";

import { modules } from "./src/modules";
import { integrations } from "./src/integrations";
import { apiOverviewSection } from "./src/apis";
import { guides } from "./src/guides";

/**
 * Creating a sidebar enables you to:
 - create an ordered group of docs
 - render a sidebar for each doc of that group
 - provide next/previous navigation

 The sidebars can be generated from the filesystem, or explicitly defined here.

 Create as many sidebars as you want.
 */

//  Detect duplicates in setup
modules.some((section) => {
  return integrations.some((plugin) => {
    if (plugin.isPackage && section.dir === plugin.dir) {
      throw new Error(`Detected duplicate section directories between integrations and modules: ${section.dir}`);
    }
    return false;
  });
});

const sidebars: SidebarsConfig = {
  // Docs
  ...modules.reduce((acc, section) => {
    acc[section.dir] = [{ type: "autogenerated", dirName: section.dir }];
    return acc;
  }, {}),
  // Guides
  ...guides.reduce((acc, section) => {
    acc[`guides/${section.dir}`] = [{ type: "autogenerated", dirName: `guides/${section.dir}` }];
    return acc;
  }, {}),
  // Integrations
  ...integrations.reduce((acc, section) => {
    acc[`integrations/${section.dir}`] = [{ type: "autogenerated", dirName: `integrations/${section.dir}` }];
    return acc;
  }, {}),
  // API
  ...[
    apiOverviewSection,
    ...modules.filter((section) => section.isPackage),
    ...integrations.filter((section) => section.isPackage),
  ].reduce((acc, section) => {
    const dir = section.dir.includes("integrations")
      ? section.dir.replace("integrations/", "api/")
      : `api/${section.dir}`;

    acc[dir] = [{ type: "autogenerated", dirName: dir }];
    return acc;
  }, {}),
};

// eslint-disable-next-line import/no-default-export
export default sidebars;
